package StudyCode.summary.mycollection;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddsAPaddingToTheGivenArraySuchThatANewArrayWithTheGivenLengthIsGenerated {
	public static void main(String[] argv) {
		byte[] array = new byte[]{34, 35, 36, 37, 37};
		byte value = 2;
		int newLength = 8;
		System.out.println(java.util.Arrays.toString(padArray(array, value, newLength)));
	}

	public static byte[] padArray(byte[] array, byte value, int newLength) {
		int length = array.length;
		int paddingLength = newLength - length;

		if (paddingLength < 1) {
			return array;
		} else {
			byte[] padding = new byte[paddingLength];
			Arrays.fill(padding, value);

			return concatenate(array, padding);
		}

	}

	public static byte[] concatenate(byte[] a, byte[] b) {
		int lengthA = a.length;
		int lengthB = b.length;

		byte[] concat = new byte[lengthA + lengthB];

		System.arraycopy(a, 0, concat, 0, lengthA);
		System.arraycopy(b, 0, concat, lengthA, lengthB);

		return concat;
	}
}
